import Service from "../../model/service";
import User from "../../model/user"; 
import Rating from '../../model/rating'
import serviceType from "../../enum/service-type";
import ServiceStatus from "../../enum/service-status";
import ServiceAction from "../../enum/service-action";
import {getEventParam} from "../../utils/util"
import cache from "../../enum/cache";
const rating = new Rating()
// pages/service-detail/service-detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    service:null,
    service_id:null,
    isPublisher:false,
    ratingList:[],
    serviceTypeEnum: serviceType,
    ServiceStatusEnum:ServiceStatus,
    loading:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    // console.log(options);
    this.data.service_id =  options.service_id; 
    await this._getService(); 
    // 获取服务评价
    await this._getServiceRatingList();  
    this._checkRole();
    this.setData({
      loading:false
    })
  },
   /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  async _getService(){
    let service = await Service.getServiceById(this.data.service_id);  
    this.setData({service}) 
  },

  async _getServiceRatingList(){
    // 如果是正在找 就不加载 评价列表
    if(this.data.service.type === serviceType.SEEK){
      return;
    }
     let ratingList = await rating.reset().getServiceRatingList(this.data.service_id); 
     this.setData({ratingList});
  },

  // 更新操作
  async handleUpdateStatus(e){
    let action = getEventParam(e,'action') 
    let content = this._generateModalContent(action); 
    const res = await wx.showModal({
      title:"注意",
      content,
      showCancel:true, 
    })
    // console.log(res);
    if(!res.confirm){
      return 
    }

    // 确定的操作
    // await Service.updateServiceStatus(this.data.service_id,action);
     console.log('暂未发起请求...',action);
    //  重新获取页面内容
    await this._getService();
  
  },
  // 修改服务
  handleEditService(){
    console.log('修改服务'); 
    const service = JSON.stringify(this.data.service);
    wx.navigateTo({
      url: `/pages/server-edit/server-edit?service=${service}`,
    })
  },
  // 联系对方
  handleChat(){
    console.log('联系对方');  
    const targetUserId = this.data.service.publisher.id;
    const service = JSON.stringify(this.data.service);
    wx.navigateTo({
      url: `/pages/conversation/conversation?targetUserId=${targetUserId}&service=${service}`,
    })
  },
  // 立即预约
  handleOrder(){
    console.log(4); 
    if(!wx.getStorageSync(cache.TOKEN)){
        wx.navigateTo({
          url: `/pages/login/login`,
          events:{
            login:()=>{
               this._checkRole()
            }
          }
        }) 
        return 
    }

    const service = JSON.stringify(this.data.service);
    wx.navigateTo({
      url: `/pages/order/order?service=${service}`,
    }) 

  },

  _generateModalContent(action){
    let content;  
    switch (action) {
        case ServiceAction.PAUSE:
            content = '暂停后服务状态变为“待发布”，' +
                '可在个人中心操作重新发布上线，' +
                '是否确认暂停发布该服务？'
            break;
        case ServiceAction.PUBLISH:
            content = '发布后即可在广场页面中被浏览到，是否确认发布？'
            break;
        case ServiceAction.CANCEL:
            content = '取消后不可恢复，需要重新发布并提交审核；' +
                '已关联该服务的订单且订单状态正在进行中的，仍需正常履约；' +
                '是否确认取消该服务？'
            break;
    }

    return content
  },

  _checkRole(){
    let userInfo = User.getUserInfoByLocal();
    // userInfo = {
    //   id:1
    // } 
    if(userInfo && userInfo.id === this.data.service.publisher.id){
      this.setData({
        isPublisher:true
      })
    } 
  } ,
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  async onReachBottom() {
    if(!rating.hasMoreData){
      return 
    } 
    const ratingList = await rating.getServiceRatingList(this.data.service_id);
    this.setData({
      ratingList
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})